package com.neusoft.elmboot.controller;


import com.neusoft.elmboot.utils.VerifyCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 随机数的控制器
 */
@Controller
public class VerifyCodeController {

    @Autowired
    private VerifyCodeUtil verifyCodeUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/geneCode")
    public void generateCode(HttpServletResponse response) throws IOException {
        //1、 生成随机码的图片，并且显示到页面输出流
        VerifyCodeUtil.output(verifyCodeUtil.getImage(), response.getOutputStream());
        // 2、将随机数的文本内容，保存到Redis中，有效期：5分钟。
        System.out.println("----------随机数的文本内容：" + verifyCodeUtil.getText());
        redisTemplate.opsForValue().set("elm:user:login:verifycode",verifyCodeUtil.getText(),
                5*60, TimeUnit.SECONDS);

    }

}
